<template>
  <el-dialog :title="form.id ? $t('OrderRefund.view') : $t('common.addBtn')" v-model="visible"
    :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="120px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item prop="uid" :label="$t('OrderRefund.uid')">
            <el-input disabled v-model="form.uid" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="shopId" :label="$t('OrderRefund.shopId')">
            <el-input disabled v-model="form.shopId" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="orderId" :label="$t('OrderRefund.orderId')">
            <el-input disabled v-model="form.orderId" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="orderItemId" :label="$t('OrderRefund.orderItemId')">
            <el-input disabled v-model="form.orderItemId" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="orderAmount" :label="$t('OrderRefund.orderAmount')">
            <el-input disabled v-model="form.orderAmount" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="payMethod" :label="$t('OrderRefund.payMethod')">
            <el-input disabled v-model="form.payMethod" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="flowTradeNo" :label="$t('OrderRefund.flowTradeNo')">
            <el-input disabled v-model="form.flowTradeNo" placeholder="" clearable
              show-word-limit :maxlength="64" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="outRefundNo" :label="$t('OrderRefund.outRefundNo')">
            <el-input disabled v-model="form.outRefundNo" placeholder="" clearable
              show-word-limit :maxlength="64" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="state" :label="$t('OrderRefund.state')">

            <el-select disabled v-model="form.state">
              <el-option label="退款中" :value="0"></el-option>
              <el-option label="退款成功" :value="1"></el-option>
              <el-option label="退款失败" :value="-1"></el-option>
            </el-select>

            <!-- <el-input disabled v-model="form.state" placeholder="" clearable
              show-word-limit /> -->
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="processState" :label="$t('OrderRefund.processState')">

            <el-select disabled v-model="form.processState">
              <el-option label="待审核" :value="0"></el-option>
              <el-option label="通过" :value="1"></el-option>
              <el-option label="拒绝" :value="2"></el-option>
            </el-select>
            <!-- <el-input disabled v-model="form.processState" placeholder="" clearable
              show-word-limit /> -->
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="num" :label="$t('OrderRefund.num')">
            <el-input disabled v-model="form.num" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="amount" :label="$t('OrderRefund.amount')">
            <el-input disabled v-model="form.amount" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="applyType" :label="$t('OrderRefund.applyType')">
            <el-input disabled v-model="form.applyType" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="pics" :label="$t('OrderRefund.pics')">
            <el-input disabled v-model="form.pics" placeholder="" clearable show-word-limit
              :maxlength="255" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="reason" :label="$t('OrderRefund.reason')">
            <el-input disabled v-model="form.reason" placeholder="" clearable
              show-word-limit :maxlength="255" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="rejectReason" :label="$t('OrderRefund.rejectReason')">
            <el-input disabled v-model="form.rejectReason" placeholder="" clearable
              show-word-limit :maxlength="255" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="createTime" :label="$t('OrderRefund.createTime')">
            <el-input disabled v-model="form.createTime" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item prop="approvedTime" :label="$t('OrderRefund.approvedTime')">
            <el-input disabled v-model="form.approvedTime" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="rejectTime" :label="$t('OrderRefund.rejectTime')">
            <el-input disabled v-model="form.rejectTime" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="refundTime" :label="$t('OrderRefund.refundTime')">
            <el-input disabled v-model="form.refundTime" placeholder="" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <!-- <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
      </span>
    </template> -->
  </el-dialog>
</template>

<script setup lang="ts" name="OrderRefundDialog">
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/bizs/OrderRefund'
const emit = defineEmits(['refresh']);

// 定义i18n
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

// 定义字典
// import { useDict } from '/@/hooks/dict';
// const { biz_order_refund_state, biz_order_refund_process_state, biz_order_refund_apply_type, biz_order_pay_method } = useDict('biz_order_refund_state', 'biz_order_refund_process_state', 'biz_order_refund_apply_type', 'biz_order_pay_method')

// 定义变量内容
const visible = ref(false)
const loading = ref(false)
const dataFormRef = ref();

// 提交表单数据
const form = reactive({
  id: '',
  uid: '',
  shopId: '',
  orderId: '',
  orderItemId: '',
  orderAmount: '',
  payMethod: '',
  flowTradeNo: '',
  outRefundNo: '',
  state: '',
  processState: '',
  num: '',
  amount: '',
  applyType: '',
  pics: '',
  reason: '',
  rejectReason: '',
  approvedTime: '',
  rejectTime: '',
  refundTime: '',
  createTime: ""
});

// 定义校验规则
// import { rule } from '/@/utils/validate';
const dataRules = ref({
  uid: [{ required: false, message: t('OrderRefund.inputUidTip'), trigger: 'blur' }],
  shopId: [{ required: false, message: t('OrderRefund.inputShopIdTip'), trigger: 'blur' }],
  orderId: [{ required: false, message: t('OrderRefund.inputOrderIdTip'), trigger: 'blur' }],
  orderItemId: [{ required: false, message: t('OrderRefund.inputOrderItemIdTip'), trigger: 'blur' }],
  orderAmount: [{ required: false, message: t('OrderRefund.inputOrderAmountTip'), trigger: 'blur' }],
  payMethod: [{ required: false, message: t('OrderRefund.inputPayMethodTip'), trigger: 'blur' }],
  flowTradeNo: [{ required: false, message: t('OrderRefund.inputFlowTradeNoTip'), trigger: 'blur' }],
  outRefundNo: [{ required: false, message: t('OrderRefund.inputOutRefundNoTip'), trigger: 'blur' }],
  state: [{ required: false, message: t('OrderRefund.inputStateTip'), trigger: 'blur' }],
  processState: [{ required: false, message: t('OrderRefund.inputProcessStateTip'), trigger: 'blur' }],
  num: [{ required: false, message: t('OrderRefund.inputNumTip'), trigger: 'blur' }],
  amount: [{ required: false, message: t('OrderRefund.inputAmountTip'), trigger: 'blur' }],
  applyType: [{ required: false, message: t('OrderRefund.inputApplyTypeTip'), trigger: 'blur' }],
  pics: [{ required: false, message: t('OrderRefund.inputPicsTip'), trigger: 'blur' }],
  reason: [{ required: false, message: t('OrderRefund.inputReasonTip'), trigger: 'blur' }],
  rejectReason: [{ required: false, message: t('OrderRefund.inputRejectReasonTip'), trigger: 'blur' }],
  approvedTime: [{ required: false, message: t('OrderRefund.inputApprovedTimeTip'), trigger: 'blur' }],
  rejectTime: [{ required: false, message: t('OrderRefund.inputRejectTimeTip'), trigger: 'blur' }],
  refundTime: [{ required: false, message: t('OrderRefund.inputRefundTimeTip'), trigger: 'blur' }],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取信息
  if (id) {
    form.id = id
    initFormData(id)
  }
};

// 初始化表单
const initFormData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 提交表单
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => { });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? t('common.editSuccessText') : t('common.addSuccessText'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 暴露变量
defineExpose({
  openDialog
});
</script>